<?php
	require_once('connection/connector.php');
	
	$connect = new DbConnector();
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>
			Check if someone is 18 years old
		</title>
		<meta charset="utf-8">
	</head>
	<body>
		<header>
			<hgroup>
				<h1>
					Year Validation
				</h1>
			</hgroup>
		</header>
		<section>
			<article>
				<header>
					<h2>
						Choose your birth date : 
					</h2>
				</header>
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=2" method="POST">
					<input type="number" min="1900" max="2012" name="year" autofocus="autofocus" value="2000" /> : 
					<input type="number" min="1" max="12" name="month" value="6" /> : 
					<input type="number" min="1" max="31" name="day" value="15" />
					<br /><br />
					<input type="submit" value="Check" />
				</form>
				<?php
					$action = $_GET['action'];
					
					if($action == 2)
					{
						$day = $_POST['day'];
						$month = $_POST['month'];
						$year = $_POST['year'];
						
						$today = getdate();
						
						$change_date = '08-12-1992';
						
						$insert = "INSERT INTO validations (dp_date) VALUES ('".$day.'-'.$month.'-'.$year."')";
					
						if(isset($day, $month, $year) && $year != 0 && $month != 0 && $day != 0)
						{
							if($year + 18 < $today['year'])
							{
								echo "You are above 18.";
								
								$connect->query($insert);
							}
							if(($year + 18 == $today['year']))
							{
								if($month < $today['mon'])
								{
									echo "You are above 18.";
									
									$connect->query($insert);
								}
								if($month == $today['mon'])
								{
									if($day <= $today['mday'])
									{
										echo "You are above 18.";
										
										$connect->query($insert);
									}
								}
							}
						}
						
						$update = "UPDATE validations SET dp_date = '$change_date' WHERE id = LAST_INSERT_ID() - 1";
						
						$update = $connect->query($update);
					}
				?>
			</article>
		</section>
	</body>
</html>